﻿@model MenuModel

@using SmartStore.Collections;
@using SmartStore.Web.Framework.UI;

@{
    Layout = "";

    var currentNode = ViewBag.SelectedNode as TreeNode<MenuItem>;

    if (Model.Root == null || currentNode == null)
    {
        return;
    }
}

@functions {
    private string GetCssClassForNode(TreeNode<MenuItem> node, NodePathState state)
    {
        var liClasses = new List<string>();
        
        if (node.Depth <= 1 && node.HasChildren)
        {
            liClasses.Add(node.Depth > 0 ? "dropdown-submenu" : "dropdown");
        }

        if ((state & NodePathState.Expanded) == NodePathState.Expanded)
        {
            liClasses.Add("expanded");
        }

        if ((state & NodePathState.Selected) == NodePathState.Selected)
        {
            liClasses.Add("selected");
        }

        return liClasses.Count > 0 ? String.Join(" ", liClasses) : null;
    }
}

<div class="category-container">
	@if (currentNode.Depth > 0)
	{
		<div class="category-info">
			<h3 @Html.LanguageAttributes(currentNode.Value.Rtl, WorkContext.WorkingLanguage) class="category-name">@currentNode.Value.Text</h3>
		</div>
	}

    <ul class="ocm-nav list-unstyled">
        @if (currentNode.Depth > 0)
        {
            <li class="navigate-back ocm-item" data-id="@currentNode.Parent.Value.EntityId" data-ajax="true">
                <a href="@currentNode.Parent.Value.GenerateUrl(this.Url)" class="ocm-link">
                    <i class="fa fa-angle-left"></i>
                    <span>@T("Common.Back")</span>
                </a>
            </li>

            <li class="show-current-cat ocm-item" data-id="@currentNode.Value.EntityId" data-ajax="false">
                <a href="@currentNode.Value.GenerateUrl(this.Url)" class="ocm-link">
                    @T("Offcanvas.Menu.ShowCurrentCat", currentNode.Value.Text)
                </a>
            </li>
        }

        @foreach (var node in currentNode.Children)
        {
            var cat = node.Value;
            var itemState = node.GetNodePathState(Model.Path);

            <li class="ocm-item@(node.HasChildren ? " has-children" : "") @(GetCssClassForNode(node, itemState))" 
				data-id="@cat.EntityId" 
				data-ajax="@(node.HasChildren ? "true" : "false")">
                <a href="@node.Value.GenerateUrl(this.Url)" class="ocm-link has-count" @Html.Attr("data-count", cat.ElementsCount.ToString(), cat.ElementsCount > 0)>
					<span @Html.LanguageAttributes(cat.Rtl, WorkContext.WorkingLanguage)>@cat.Text</span>
                </a>
            </li>
        }
    </ul>
</div>